import xadmin
from xadmin import views
from . import models

class BaseSetting(object):
    """xadmin的基本配置"""
    enable_themes = True  # 开启主题切换功能
    use_bootswatch = True

xadmin.site.register(views.BaseAdminView, BaseSetting)

class GlobalSettings(object):
    """xadmin的全局配置"""
    site_title = "美多商城运营管理系统"  # 设置站点标题
    site_footer = "美多商城集团有限公司"  # 设置站点的页脚
    menu_style = "accordion"  # 设置菜单折叠

xadmin.site.register(views.CommAdminView, GlobalSettings)

class SKUAdmin(object):
    model_icon = 'fa fa-gift'
    # 列表的字段列表
    list_display = ['id', 'name', 'price', 'stock', 'sales', 'comments']
    # 字段过滤
    list_filter = ['category']
    # 设置可编辑字段
    list_editable = ['price', 'stock']
    # 点击显示详情的字段
    show_detail_fields = ['name']
    # 支持到处数据的格式
    list_export = ['xls', 'csv', 'xml']

    def save_models(self):
        self.new_obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(self.new_obj.id)

xadmin.site.register(models.SKU, SKUAdmin)

####################################################################
from celery_tasks.html.tasks import generate_static_list_search_html

# Register your models here.
# admin.site.register(模型类,站点管理类)

class GoodsCategoryAdmin(object):
    def save_models(self):
        """
        钩子方法，在运营站点中编辑/添加数据时，会自动执行代码
        """
        self.new_obj.save() # 保存本次提交的操作

        # 保存提交操作以后，执行异步任务
        generate_static_list_search_html.delay()

    def delete_model(self):
        self.obj.delete()

        generate_static_list_search_html.delay()

class GoodsChannelAdmin(object):
    def save_models(self):
        """
        钩子方法，在运营站点中编辑/添加数据时，会自动执行代码
        """
        self.new_obj.save() # 保存本次提交的操作

        # 保存提交操作以后，执行异步任务
        generate_static_list_search_html.delay()

    def delete_model(self):
        """
        钩子方法，在删除数据的时候自动执行
        :return:
        """
        self.obj.delete()

        # 保存提交操作以后，执行异步任务
        generate_static_list_search_html.delay()


class SKUSpecificationAdmin(object):
    def save_models(self):
        self.new_obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(self.new_obj.sku.id)

    def delete_model(self):
        sku_id = self.obj.sku.id
        self.obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)


class SKUImageAdmin(object):
    def save_models(self):
        self.new_obj.save()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(self.new_obj.sku.id)

        # 设置SKU默认图片
        sku = self.new_obj.sku
        if not sku.default_image_url:
            sku.default_image_url = self.new_obj.image.url
            sku.save()

    def delete_model(self):
        sku_id = self.obj.sku.id
        self.obj.delete()
        from celery_tasks.html.tasks import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)

xadmin.site.register(models.GoodsCategory,GoodsCategoryAdmin)
xadmin.site.register(models.GoodsChannel,GoodsChannelAdmin)
xadmin.site.register(models.Goods)
xadmin.site.register(models.Brand)
xadmin.site.register(models.GoodsSpecification)
xadmin.site.register(models.SpecificationOption)
xadmin.site.register(models.SKUSpecification,SKUSpecificationAdmin)
xadmin.site.register(models.SKUImage,SKUImageAdmin)
